﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hospital.Models
{
    public class ZaprReposite
    {
        private HostleDataContext _datacontext;

        public ZaprReposite(HostleDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Zapro> GetZapros()
        {
            return _datacontext.Zapros.OrderBy(z => z.DataPost);
        }

        public Zapro GetOneZapros(int id)
        {
            return _datacontext.Zapros.SingleOrDefault(z => z.Id_Zap == id);
        }

        public Zapro AddResultZapros(DateTime datavip, string status)
        {
            Zapro z = new Zapro { DataVip = datavip, Status = status };
            _datacontext.Zapros.InsertOnSubmit(z);
            _datacontext.SubmitChanges();
            return z;
        }

        public Zapro AddZapros(int id, string spec, Doctor d)
        {
            Zapro z = new Zapro { Man_Id = id, SpecDoc = spec, Doc_Id = 1, Spr_Id = 1 };
            z.DataPost = DateTime.Now;
            z.DataVip = DateTime.Now;
            z.Status = "Обслуживается";
            z.Doc_Id = d.Id_Doc;
            _datacontext.Zapros.InsertOnSubmit(z);
            _datacontext.SubmitChanges();
            return z;
        }

        public void UpdateZapros(Zapro z)
        {
            Zapro dbZap = GetOneZapros(z.Id_Zap);
            dbZap.Spr_Id = z.Spr_Id;
            _datacontext.SubmitChanges();
        }

        public void UpdateZaprosDoc(Zapro z)
        {
            Zapro dbZap = GetOneZapros(z.Id_Zap);
            dbZap.Status = "Выписан";
            dbZap.DataVip = DateTime.Now;
            _datacontext.SubmitChanges();
        }

        public void DeleteZapros(int id)
        {
            Zapro dbZap = GetOneZapros(id);
            _datacontext.Zapros.DeleteOnSubmit(dbZap);
            _datacontext.SubmitChanges();
        }
    }
}